import ast


class Config:
    def __init__(self, path):
        self.path = path
        self.label = ''
        self.test_size = 0.0
        self.random_state = 22
        self.n_estimators = 100
        self.max_depth = 5
        self.grid_search_n_estimators = [10,20]
        self.grid_search_random_state = [9]
        self.grid_search_cv = 3
        self.grid_search_verbose = 3
        self.grid_search_max_depth = [2,3,4,5]
        # 学习率
        self.learning_rate = 0.05
        # 防止过拟合 0.8-1.0
        self.subsample = 0.8
        # 每棵树随机采样的比例
        self.colsample_bytree = 0.8
        # 叶子节点划分的最小损失减值 用于剪枝
        self.gamma = 2
        self._load_config()

    def _load_config(self):
        with open(self.path, 'r', encoding='utf-8') as f:
            content = f.read().strip()
            try:
                config_dict = ast.literal_eval(content)
                self.test_size = config_dict.get('test_size', 0.0)
                self.random_state = config_dict.get('random_state', 22)
                self.n_estimators = config_dict.get('n_estimators', 100)
                self.max_depth = config_dict.get('max_depth', 5)
                self.grid_search_n_estimators = config_dict.get('grid_search_n_estimators', [10,20])
                self.grid_search_random_state = config_dict.get('grid_search_random_state', [9])
                self.grid_search_cv = config_dict.get('grid_search_cv', 3)
                self.grid_search_verbose = config_dict.get('grid_search_verbose', 3)
                self.grid_search_max_depth = config_dict.get('grid_search_max_depth', [2,3,4,5])
                self.learning_rate = config_dict.get('learning_rate', 0.05)
                self.subsample = config_dict.get('subsample', 0.8)
                self.colsample_bytree = config_dict.get('colsample_bytree', 0.8)
                self.gamma = config_dict.get('gamma', 2)
            except Exception as e:
                print(f"配置加载错误：{e}")
